package tacos.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import tacos.domain.Order;
import tacos.domain.User;
import tacos.repository.OrderRepository;
import tacos.repository.jpa.JpaOrderRepository;
import tacos.repository.jpa.UserRepository;
import tacos.web.OrderProps;

import javax.validation.Valid;
import java.security.Principal;

@Slf4j
@Controller
@RequestMapping("/orders")
@SessionAttributes("order")
public class OrderController {

    private UserRepository userRepo;

    private JpaOrderRepository orderRepo;

    private OrderProps props;

    public OrderController(JpaOrderRepository orderRepo, UserRepository userRepo, OrderProps props) {
        this.orderRepo = orderRepo;
        this.userRepo = userRepo;
        this.props = props;
    }

    @GetMapping("/current")
    public String orderForm() {
        return "orderForm";
    }

    @GetMapping
    public String ordersForUser(@AuthenticationPrincipal User user,
                                Model model) {
        Pageable pageable = PageRequest.of(0, props.getPageSize());
        model.addAttribute(
                "orders", orderRepo.findByUserOrderByPlacedAtDesc(user, pageable)
        );
        return "orderList";
    }

//    @PostMapping
//    public String processOrder(@Valid Order order,
//                               Errors errors,
//                               SessionStatus sessionStatus,
//                               Principal principal) {
//        if (errors.hasErrors()) {
//            return "orderForm";
//        }
//        log.info("Order submitted: " + order);
//        User user = userRepo.findByUsername(principal.getName());
//        order.setUser(user);
//        orderRepo.save(order);
//        sessionStatus.setComplete();
//        return "redirect:/";
//    }

//    @PostMapping
//    public String processOrder(@Valid Order order,
//                               Errors errors,
//                               SessionStatus sessionStatus,
//                               Authentication authentication) {
//        if (errors.hasErrors()) {
//            return "orderForm";
//        }
//        log.info("Order submitted: " + order);
//        User user = (User) authentication.getPrincipal();
//        order.setUser(user);
//        orderRepo.save(order);
//        sessionStatus.setComplete();
//        return "redirect:/";
//    }

//    @PostMapping
//    public String processOrder(@Valid Order order,
//                               Errors errors,
//                               SessionStatus sessionStatus,
//                               @AuthenticationPrincipal User user) {
//        if (errors.hasErrors()) {
//            return "orderForm";
//        }
//        log.info("Order submitted: " + order);
//        order.setUser(user);
//        orderRepo.save(order);
//        sessionStatus.setComplete();
//        return "redirect:/";
//    }

    @PostMapping
    public String processOrder(@Valid Order order,
                               Errors errors,
                               SessionStatus sessionStatus) {
        if (errors.hasErrors()) {
            return "orderForm";
        }
        log.info("Order submitted: " + order);
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        order.setUser(user);
        orderRepo.save(order);
        sessionStatus.setComplete();
        return "redirect:/";
    }

}
